package main.java.com.fxy.controller;


import main.java.com.fxy.pojo.Cost;
import main.java.com.fxy.pojo.Page;
import main.java.com.fxy.service.CostService;
import main.java.com.fxy.service.impl.CostServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@WebServlet("/homeCostServlet")
public class CostController extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private CostService costService = new CostServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        System.out.println("action=" + action);
        if("add".equals(action)) {
            add(request, response);
        } else if ("delete".equals(action)) {
            delete(request, response);
        } else if ("update".equals(action)) {
            update(request, response);
        } else if ("query".equals(action)) {
            query(request, response);
        } else if ("list".equals(action)) {
            list(request, response);
        }else if ("getHomeCostById".equals(action)) {
            getHomeCostById(request, response);
        }else if("getDetail".equals(action)) {
            getDetail(request, response);
        }else if("listByPage".equals(action)) {
            listByPage(request, response);
        }
    }

    private void getDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        Cost homeCost = costService.getCostById(id);
        req.setAttribute("homeCost", homeCost);
        String givenSizeStr = req.getParameter("givenSize");
        Integer givenSize = (givenSizeStr != null && !givenSizeStr.isEmpty()) ? Integer.parseInt(givenSizeStr) : 5; // 默认每页显示5条记录
        req.getRequestDispatcher("/detail.jsp?givenSize="+givenSize).forward(req, resp);
    }

    protected void getHomeCostById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        Cost homeCost = costService.getCostById(id);
        req.setAttribute("homeCost", homeCost);
        String givenSizeStr = req.getParameter("givenSize");
        Integer givenSize = (givenSizeStr != null && !givenSizeStr.isEmpty()) ? Integer.parseInt(givenSizeStr) : 5; // 默认每页显示5条记录
        req.getRequestDispatcher("/cost_edit.jsp?givenSize="+givenSize).forward(req, resp);
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cost cost = new Cost();
        cost.setName(req.getParameter("name"));
        cost.setMoney(Double.valueOf(req.getParameter("money")));
        cost.setDate(new Date());
        cost.setDetail(req.getParameter("detail"));
        HttpSession session = req.getSession();
        String user = session.getAttribute("user").toString();
        Integer id = costService.getIdByName(user);
        cost.setUserId(id);
        costService.addCost(cost);
        // 获取 givenSize 参数
        String givenSizeStr = req.getParameter("givenSize");
        Integer givenSize = (givenSizeStr != null && !givenSizeStr.isEmpty()) ? Integer.parseInt(givenSizeStr) : 5; // 默认每页显示5条记录

        resp.sendRedirect(req.getContextPath() + "/homeCostServlet?action=listByPage&currentPage=1&givenSize=" + givenSize);
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        costService.deleteById(id);
        // 获取 givenSize 参数
        String givenSizeStr = req.getParameter("givenSize");
        Integer givenSize = (givenSizeStr != null && !givenSizeStr.isEmpty()) ? Integer.parseInt(givenSizeStr) : 5; // 默认每页显示5条记录

        resp.sendRedirect(req.getContextPath() + "/homeCostServlet?action=listByPage&currentPage=1&givenSize=" + givenSize);
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id= Integer.valueOf(req.getParameter("id"));
        String name = req.getParameter("name");
        Double money = Double.valueOf(req.getParameter("money"));
        String detail = req.getParameter("detail");
        Cost cost = new Cost(id, name, money,new Date(),detail);
        costService.update(cost);
        // 获取 givenSize 参数
        String givenSizeStr = req.getParameter("givenSize");
        Integer givenSize = (givenSizeStr != null && !givenSizeStr.isEmpty()) ? Integer.parseInt(givenSizeStr) : 5; // 默认每页显示5条记录

        resp.sendRedirect(req.getContextPath() + "/homeCostServlet?action=listByPage&currentPage=1&givenSize=" + givenSize);

    }
    protected void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("1");
        HttpSession session = req.getSession();
        String user = session.getAttribute("user").toString();
        Integer id = costService.getIdByName(user);
        String keyword=req.getParameter("keyword");
        // 获取当前页数
        String currentPageStr = req.getParameter("currentPage");
        Integer currentPageNum = getCurrentPage(currentPageStr);
        System.out.println("2");
        // 创建分页对象
        Page<Cost> pageBean = new Page<>();
        String givenSizeStr = req.getParameter("givenSize");
        Integer givenSize = (givenSizeStr != null && !givenSizeStr.isEmpty()) ? Integer.parseInt(givenSizeStr) : 5; // 默认每页显示5条记录
        try {
            // 查询并设置分页数据
            pageBean.setPageSize(givenSize);
            pageBean.selectUserListByKeyword(currentPageNum, pageBean, id, keyword,givenSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        req.setAttribute("queryPageBean", pageBean);
        req.getRequestDispatcher("/query.jsp").forward(req, resp);
    }
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String user = session.getAttribute("user").toString();
        Integer id = costService.getIdByName(user);

        List<Cost> homeCost = costService.list(id);
        req.setAttribute("homeCost", homeCost);
        req.getRequestDispatcher("/manager.jsp").forward(req, resp);
    }
    protected void listByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPageStr = req.getParameter("currentPage");
        Integer currentPageNum = getCurrentPage(currentPageStr);
        HttpSession session = req.getSession();
        String user = session.getAttribute("user").toString();
        Integer id = costService.getIdByName(user);
        String givenSizeStr = req.getParameter("givenSize");
        Integer givenSize = (givenSizeStr != null && !givenSizeStr.isEmpty()) ? Integer.parseInt(givenSizeStr) : 5; // 默认每页显示5条记录
        Page<Cost> pageBean = new Page<>();
        try {
            pageBean.setPageSize(givenSize);
            pageBean.selectUserListByPage(currentPageNum,pageBean,id,givenSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        req.setAttribute("pageBean", pageBean);
        req.getRequestDispatcher("/homeCostServlet?action=list").forward(req,resp);
    }

    private Integer getCurrentPage(String currentPagestr) {
        if (null == currentPagestr) {
            currentPagestr = "1";
        }
        return Integer.valueOf(currentPagestr);
    }
}
